package com.woniuxy.handler;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.woniuxy.pojo.User;
import com.woniuxy.service.UserService;

@Controller
@RequestMapping("/user")
public class UserHandler {
	@Resource
	private UserService userService;
	
	@RequestMapping("/login")
	public String login(User user,Boolean rm){
		//获取subject对象
		Subject currentUser = SecurityUtils.getSubject();
		//判断当前用户是否已经认证过了
		if(!currentUser.isAuthenticated()){
			//进行认证
			UsernamePasswordToken token = new UsernamePasswordToken(user.getAccount(), user.getPass());
			
			//remember me
			if(rm != null){
				token.setRememberMe(true);
			}
			
			try{
				currentUser.login(token);//进行认证
				System.out.println("认证成功！");
				return "../main.html";
			}catch(UnknownAccountException e){
				System.out.println("用户不存在");
				return "../index.html";
			}	catch(IncorrectCredentialsException e){
				System.out.println("密码不正确");
				return "../index.html";
			}	catch(LockedAccountException e){
				System.out.println("账号被锁定");
				return "../index.html";
			}	catch(AuthenticationException e){
				System.out.println("其他异常");
				return null;
			}
		}
		return null;	
	}
}
